<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=array(
		'0'=>'待付款_NO pay',
		'1'=>'待发货_NO send',
		'2'=>'待收货_NO get',
		'3'=>'已完成_Payment complete',
		'4'=>'已关闭_exchange',
		'5'=>'申请维权_Return',
		'6'=>'已维权',
		'-1'=>'已关闭'
);
$return['send_status']=$this->send_status;
$return['status2']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['paytype']=array(
		'credit'=>'余额支付_Balance pay',
		'wechat'=>'微信支付_Wechat pay',
		'alipay'=>'支付宝支付_Alipay',
		'yinlian'=>'银联支付',
		'hdfk'=>'货到付款',
);
$return['paytype']['peerpay']='找人代付';
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
//执行任务
$this->model('task')->orderup(true);
$install = check_account_permission('jy_peisong');
if($install){
	model('jy_peisong/order')->order_checktime();
}
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0);
    $orderby='id desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$tip='所有订单显示，列表商品只显示第一个！';
	$tip .='</br><red>【注意】：本场景已开启独立商品展示，只显示本场景下的订单！</red>';
	$tip .='</br>【<red>订单时间显示不准确？</red>】：可能服务器的时间不准确，联系管理员同步一下服务器网络时间！';
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			$params['plugin_name']=$onepluginname;
			$return['params']['plugin_name']=$onepluginname;
	}else{
			//$params['plugin_name']='';
	}
	if($params['status']==1){
		$batch=array(
			array('type'=>'post','url'=>'jy_weishop/order.list/setfa/','title'=>'一键发货'),
		);
	}
	$return['list']=$this->getslice('order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$goods=pdo_get('jy_weishop_order_goods',array('orderid'=>$item['id']),array('goodsid','total'));
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid']),array('title','image','unit'));
		$item['total']=$goods['total'];
		$item['title']=$goodsinfo['title'];
		$item['image']=$goodsinfo['image'];
		$item['unit']=$goodsinfo['unit'];
		$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid']),'title');
		$item['expressid']=pdo_getcolumn('jy_weishop_order_express',array('orderid'=>$item['id']),'id');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理_Order list'),
			array('op'=>'add','title'=>'生成订单_Order info'),
		),
		'list'=>array('data'=>'list','openvoice'=>true,'openvoice_action'=>'jy_weishop/order.list/getnew/','tip'=>$tip,'edit'=>false,'total'=>$total,'psize'=>$psize,'html_to'=>'list_html','list'=>array(
			array('name'=>'ordersn','title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'storetitle','title'=>'所属门店_Store','type'=>'string'),
			array('type'=>'img_string','title'=>'商品名称/数量_Goods','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'数量：_num：','name'=>'total','unit'=>'unit')
				)
			),
			array('name'=>'price','title'=>'金额_Price','type'=>'string'),
			array('name'=>'createtime','title'=>'下单时间_order time','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付_Is pay?','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式_Pay type','type'=>'radio','options'=>'paytype'),			
		),
		'action'=>array(
			array('type'=>'modal','op'=>'setstatus','title'=>'设置状态_Set status'),
			array('type'=>'href','action'=>'order.detail','title'=>'订单详情_Detail'),
			array('action'=>'order.express/','title'=>'查看物流_Express','type'=>'modal','show'=>array('status'=>2,'expressid >'=>0)),
			array('op'=>'fa','title'=>'发货_Ship','type'=>'modal','show'=>array('status'=>1,'send_status'=>array(0,11))),
			//array('op'=>'peisong','title'=>'指定配送_Distribution','type'=>'modal','show'=>array('status'=>1,'isdish'=>1)),
			array('op'=>'print','title'=>'打印_Print','type'=>'post'),
			array('type'=>'post','op'=>'shou','title'=>'确认收货_Confirm ','show'=>array('status'=>2)),
			
		),'batch'=>$batch,
		),
		'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'status','width'=>10,'title'=>'订单状态_Status','type'=>'radio','options'=>'status'),
			array('name'=>'buytype','width'=>10,'title'=>'订单类型_Type','type'=>'radio','options'=>'buytype'),
			array('name'=>'sendtype','width'=>10,'title'=>'配送方式_Ship type','type'=>'radio','options'=>'sendtype'),
			array('name'=>'merchid','width'=>10,'title'=>'所属商户_Merch','type'=>'select','option_value'=>'id','option_title'=>'merchname','options'=>'merchs'),
			array('type'=>'choose','width'=>13,'title'=>'所属会员','name'=>'mid','action'=>'core/member.get_member/'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		)
	);
	if($params['status']==1){
		$htmls['list']['list'][]=array('name'=>'send_status','title'=>'配送状态','type'=>'radio','options'=>'send_status');
	}else{
		$htmls['list']['list'][]=array('name'=>'status','title'=>'订单状态_status','type'=>'radio','options'=>'status');
	}
	$return['htmls']=$htmls;
	if($this->plugin_exists('jy_merch')){
		$merchs=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'));
	}else{
		$merchs=array();
	}
	array_unshift($merchs,array('id'=>0,'merchname'=>'平台自营_Proprietary'));
	$return['merchs']=$merchs;
	$shopset=$this->plugin_setting('shopset');
	set_html('title',$shopset['title']);
	exi($return);
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_weishop_order',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');		
		if($item['address']){
			$address=json_decode($item['address']);
		}else{
			$address=pdo_get('core_address',array('id'=>$v['addressid'],'uniacid'=>$_SESSION['uniacid']));
		}
		$v['address']=model('core/member')->get_address_str($v['addressid']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','optionid'));
		foreach($goods as $k=>$gv){
			$goodstitle=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'title');
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$selespename='无规格';
			}
			$num=$k+1;
			$v['goodstitle'] .='【商品'.$num.'】['.$selespename.']'.$goodstitle;
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'storetitle'=>'所属门店',
		'goodstitle'=>'商品名称',
		'nickname'=>'买家昵称',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'支付状态',
		'paytype'=>'支付方式',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
if($_RQ['op']=='setfa'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择订单','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			$sendtype=pdo_getcolumn('jy_weishop_order',array('id'=>$id),'sendtype');
			if($sendtype==1){
				$this->model('order')->order_send($id);
			}else{
				$this->model('order')->order_send_virtual($id);
			}
		}
	}	
	exi('一键发货成功！');
}
if($_RQ['op']=='fa'){
	$install = check_account_permission('jy_peisong');
	if(!$install){
		exi('请联系管理员开通《配送员》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
			if($post['express_type']==0){
				if(!$post['express'] || !$post['expresssn']){
					exi('请填写快递信息','error');
				}
				$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
				$this->model('order')->order_send($id,$data);
			}elseif($post['express_type']==1){//平台配送
				if(!$post['paisong_id'] && $post['need_setpeisong']){
					exi('请选择配送员！','error');
				}
				if($post['paisong_id']){
					$params['order_id']=$id;
					$params['peisong_id']=$post['paisong_id'];
					model('jy_peisong/order')->order_sendget($params);
				}else{
					$data['send_status']=12;
					cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新配送订单！','peisong_order');
					$this->model('order')->order_send($id,$data);
				}			
				exi('订单配送处理成功！','','//list/status=1');
			}elseif($post['express_type']==2){//商家自己配送
				$this->model('order')->order_send($id);
				exi('订单配送处理成功！','','//list/status=1');
			}elseif($post['express_type']==3){//达达配送
				$res=$this->model('order/dada_api')->dada_send($post);
				if($res['state']==1){
					$data=array('opensend_type'=>1);
					$this->model('order')->order_send($id,$data);
				}else{
					exi($res['msg'],'error');
				}
			}
			
		}else{
			$this->model('order')->order_send_virtual($id,$post['content']);
		}
		
		exi('已发货！','','/order.list/list/status=1');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','sendtype','storeid','merchid','addressid','ordersn','remark','price','isdish'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单发货',
	);
	if($item['sendtype']==1){
		$citypeisong=$this->plugin_setting('citypeisong');
		$item['citypeisong_isopen']=$citypeisong['isopen'];
		$item['citypeisong_express_type']=$citypeisong['express_type']?:0;
		if($citypeisong['isopen']==1 || $item['isdish']){
			$item['express_type']=$citypeisong['express_type']?:1;
			if($item['express_type']==1){
				$tip='商品订单已打包好,由平台配送员配送！';
			}elseif($item['express_type']==2){
				$tip='商品订单已打包好,商家自己配送,无需配送员！';
			}elseif($item['express_type']==3){
				$tip='商品订单已打包好,发送给达达配送！(<red>【慎重操作】：确定要呼叫《达达配送员》过来拿货吗?</red>)';
			}
			
		}else{
			$item['express_type']=0;
			$tip='商品订单已打包好,快递发货！';
		}
		
		$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('express_type'=>0)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('express_type'=>0)),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定配送员？','show'=>array('express_type'=>1),'help'=>'不指定，则由配送员自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定配送员','name'=>'paisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('need_setpeisong'=>1),'help'=>'只能指定该门店下面的配送员，或本城市的平台配送员！'),
		);
		$return['express']=$this->express_types;
	}else{
		$goodsid=$this->pdo_getcolumn('order_goods',array('orderid'=>$id),'goodsid');
		$goods=$this->pdo_get('goods',array('id'=>$goodsid),array('title','image'));
		$item=array_merge($goods,$item);
		$htmls['edit']=array(
			array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
			array('type'=>'image','title'=>'商品图片','name'=>'image','read'=>true),
			array('type'=>'text','title'=>'发货内容','name'=>'content','help'=>'虚拟物品发货，填写发货内容！'),
			
		);
	}
	$peisongs=array();
	//多商户用户，选择平台区域配送员
	if($item['merchid']){
		$areas=pdo_getall('jy_peisong_area',array('store_id'=>0));
	}else{
		$areas=pdo_getall('jy_peisong_area',array('store_id'=>array($item['storeid'],0)));
	}
	foreach($areas as $v){
			$users=pdo_getall('jy_peisong_member',array('group_id'=>$v['id'],'status'=>1,'online >'=>0),array('mid','title'));
			foreach($users as $uv){
					$peisongs[]=$uv;
			}
	}
	$return['peisongs']=$peisongs;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='setstatus'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$status=intval($post['status']);
		$id=intval($post['id']);
		$update=array('status'=>$status,'ispay'=>intval($post['ispay']));
		if($status==3){
			$update['finishtime']=time();
		}
		pdo_update('jy_weishop_order',$update,array('id'=>$id));
		exi('设置成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','ispay','ordersn','status','storeid'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置状态',
	);
	$htmls['edit']=array(
			array('type'=>'string','title'=>'ordersn','name'=>'ordersn','read'=>true),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'status'),
			array('type'=>'radio','title'=>'是否支付','name'=>'ispay','options'=>'ispay'),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='peisong'){
	$install = check_account_permission('jy_peisong');
	if(!$install){
		exi('请联系管理员开通《配送员》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['paisong_id']){
			exi('请选择配送员！','error');
		}
		$params['order_id']=$id;
		$params['peisong_id']=$post['paisong_id'];
		model('jy_peisong/order')->order_get($params);		
		exi('指定配送员成功！','','//list/status=1');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order',array('id'=>$id),array('id','sendtype','storeid','merchid'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单指定配送',
	);
	$htmls['edit']=array(
			array('type'=>'select','title'=>'指定配送员','name'=>'paisong_id','options'=>'peisongs','option_value'=>'mid','help'=>'只能指定该门店下面的配送员'),
	);
	$peisongs=array();
	//多商户用户，选择平台区域配送员
	if($item['merchid']){
		$areas=pdo_getall('jy_peisong_area',array('store_id'=>0));
	}else{
		$areas=pdo_getall('jy_peisong_area',array('store_id'=>array($item['storeid'],0)));
	}
	foreach($areas as $v){
				$users=pdo_getall('jy_peisong_member',array('group_id'=>$v['id'],'status'=>1,'online >'=>0),array('mid','title'));
				foreach($users as $uv){
					$peisongs[]=$uv;
				}
	}
	$return['peisongs']=$peisongs;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='shou'){
	$id=intval($_RQ['id']);
	$this->model('order')->order_receive($id);
	exi('已收货','','//list/status=3');
}
if($_RQ['op']=='print'){
	$order_id=intval($_RQ['id']);
	$res=$this->model('printmsg')->storeprint('',$order_id,0,1);
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败,该订单所属门店没有添加打印任务！','error');
	}
}

if($_RQ['op']=='deleted'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>1);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'ddel','list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),				
		),
		'action'=>array(	
			array('type'=>'href','action'=>'order.detail','title'=>'订单详情'),
		),),
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['goodsid'] || (!$post['mid'] && $post['is_new']!=1)){
			exi('必须选择商品，会员等','error');
		}
		$mid=$post['mid'];
		if($post['is_new']==1){
			$member=array(
				'nickname'=>trim($post['name']),
				'mobile'=>trim($post['mobile']),
				'uniacid'=>$_SESSION['uniacid'],
				'username'=>'user_'.random(10),
				'from_os'=>get_os(),
				'is_virtual'=>1,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('core_members',$member);
			$mid=pdo_insertid();
		}
		if($post['is_en']==1){
			$post['pcd']=array(
				'countries'=>$post['countries'],
				'province'=>$post['province'],
				'city'=>$post['city'],
				'district'=>$post['district'],
			);
		}
		$data=array(
			'isdefault'=>1,
			'mobile'=>trim($post['mobile']),
			'name'=>trim($post['name']),
			'pcd'=>ijson_encode($post['pcd']),
			'address'=>trim($post['address']),
			'area_code'=>trim($post['area_code'])
		);
		$data['mid']=$mid;
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('core_address',$data);
		$addressid=pdo_insertid();
		$goods=pdo_get('jy_weishop_goods',array('id'=>$post['goodsid']));
		$goods['total']=1;
		$goods['goodsid']=$goods['id'];
		if($post['createtime']){
			$mins=rand(1,59);
			$createtime=strtotime($post['createtime'])+$mins;
			if($post['isrand']){
				$createtime +=$mins*318;
			}
		}elseif($post['isrand']){
			$rand=rand(2,30);
			$days=rand(8,30);
			$createtime=TIMESTAMP-$days*24*3600-$rand*600;
		}else{
			$createtime=TIMESTAMP;
		}
		do{
			$nonce = random(6,true);
			$ordersn=date('YmdHis',$createtime).$nonce;
		}while(pdo_getcolumn('jy_weishop_order',array('ordersn'=>$ordersn),'ordersn'));
		$new_order=array(
			'mid'=>$mid,
			'price'=>$goods['price'],
			'goodsprice'=>$goods['price'],
			'goods'=>array($goods),
			'addressid'=>$addressid,
			'sendtype'=>1,
			'type'=>1,
			'createtime'=>$createtime,
			'ordersn'=>$ordersn,
		);
		$id=$this->model('order')->create($new_order);
		if($post['isrand'] || $post['createtime']){
			$update=array('status'=>3,'ispay'=>intval($post['ispay']));
			$rand=rand(2,50);
			if($post['createtime']){
				$days=rand(3,6);
				$mins=rand(1,59)*2;
				$update['finishtime']=$createtime+$days*24*3600+$rand*600+$mins;
			}else{
				$days=$days-3;
				$update['finishtime']=TIMESTAMP-$days*24*3600-$rand*600;
			}
			if($update['finishtime']<TIMESTAMP){
				pdo_update('jy_weishop_order',$update,array('id'=>$id));
			}
		}
		exi('生成订单成功！');
	}
	$item=array(
		'countries'=>'US',
		'province'=>'',
		'city'=>'',
		'is_new'=>0,
		'is_en'=>0,
	);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'生成订单',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理_Order list'),
			array('op'=>'add','title'=>'生成订单_Order info'),
		),
		'edit'=>array(
			array('title'=>'为了便于真实性，所有生成的订单时间都是两天前的','type'=>'tip'),
			array('name'=>'isrand','title'=>'订单时间随机？','type'=>'switch'),
			array('name'=>'createtime','title'=>'指定创建时间','type'=>'time'),
			array('name'=>'is_new','title'=>'新会员？','type'=>'switch'),
			array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'core/member.get_member//is_virtual=1','choose_title'=>'选择会员','show'=>array('is_new'=>0),'help'=>'这里只能选择虚拟会员！不会显示真实会员！'),
			array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'选择商品'),
			array('name'=>'name','title'=>'收件姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'收件电话','type'=>'string'),
			array('name'=>'is_en','title'=>'外国地址？','type'=>'switch'),
			array('name'=>'pcd','title'=>'区域地址','type'=>'pcd','show'=>array('is_en'=>0)),
			array('name'=>'countries','title'=>'国家','type'=>'string','show'=>array('is_en'=>1)),
			array('name'=>'province','title'=>'州','type'=>'string','show'=>array('is_en'=>1)),
			array('name'=>'city','title'=>'城市','type'=>'string','show'=>array('is_en'=>1)),
			array('name'=>'district','title'=>'区域','type'=>'string','show'=>array('is_en'=>1)),
			array('name'=>'address','title'=>'详细地址','type'=>'string'),
			array('name'=>'area_code','title'=>'邮编','type'=>'string','show'=>array('is_en'=>1)),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));
			}
		}
	}
	exi('删除成功！','','list');
}
if($_RQ['op']=='ddel'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！','','deleted');
}
if($_RQ['op']=='getnew'){
	$params=array(
		'status'=>1,
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>intval($_RQ['merchid']),
		'api_id'=>intval($_RQ['api_id']),
		'api_pluginname'=>$_RQ['api_pluginname']?:'',
	);
	$order=pdo_get('jy_weishop_order',$params);
	if($order['id']){
		exi($order);
	}else{
		exi('none');
	}
}
?>